package io.github.loserYa.log.handler;

import com.alibaba.fastjson2.JSONObject;
import com.rabbitmq.client.Channel;
import io.github.loserYa.log.SqlBuildHandler;
import io.github.loserYa.mutil.RabbitFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.env.Environment;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 日志落地消费者
 *
 * @author loser
 * @date 2024/4/25
 */
public abstract class SaveLogHandler {

    public SaveLogHandler(ConfigurableListableBeanFactory factory, Environment environment) {
        new RabbitFactory(getPre(), factory, environment);
    }

    protected static final Logger log = LoggerFactory.getLogger(SaveLogHandler.class);

    @Resource
    protected SqlBuildHandler sqlBuildHandler;

    public List<String> buildSql(JSONObject entity) {
        return sqlBuildHandler.buildSql(entity);
    }

    abstract public String getPre();

    abstract public void bizLogSave(@Payload JSONObject msg, @Headers Map<String, Object> headers, Channel channel) throws IOException;

}
